package Datos;

import Datos.Conexion;
import Entidades.Datos;
import Interfaces.IDatos;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 *
 * @author Rafael Castelo
 */
public class DBDatos implements IDatos {
    
    private Connection connection;
    
    public DBDatos() {
        this.connection = new Conexion().getConection();
    }
    
    @Override
    public boolean ingreso(Datos dato) {
        try {
            CallableStatement statement;
            statement = connection.prepareCall("{call agregacliente (?,?,?,?,?)}");
            statement.setString(1, dato.getapellido());
            statement.setString(2, dato.getdir());
            statement.setString(3, dato.getemail());
            statement.setString(4, dato.getnombre());
            statement.setString(5, dato.gettel());
            
            statement.executeUpdate();
        } catch (Exception ex) {
            //System.out.println("error al agregar calificaciones");
            ex.printStackTrace();
            return false;
        }
        return true;
        
    }
    
    @Override
    public boolean modificar(Datos dato) {
        try {
            CallableStatement statement;
            statement = connection.prepareCall("{call modificaclientes (?,?,?,?,?,?)}");
            statement.setInt(1, dato.getid());
            statement.setString(2, dato.getapellido());
            statement.setString(3, dato.getdir());
            statement.setString(4, dato.getemail());
            statement.setString(5, dato.getnombre());
            statement.setString(6, dato.gettel());
            
            statement.executeUpdate();
        } catch (Exception ex) {
            //System.out.println("error al modificar calificaciones");
            ex.printStackTrace();
            return false;
        }
        return true;
    }
    
    @Override
    public boolean eliminar(int id) {
        try {
            CallableStatement statement;
            statement = connection.prepareCall("{call eliminacliente (?)}");
            statement.setInt(1, id);
            statement.executeUpdate();
        } catch (Exception ex) {
            //System.out.println("error al eliminar calificacion");

            return false;
        }
        return true;
    }
    
    @Override
    public ArrayList<Datos> lista(String filtro) {
        ArrayList<Datos> per = new ArrayList<Datos>();
        try {
            CallableStatement statement = connection.prepareCall("{call listacliente(?)}");
            statement.setString(1, filtro);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Datos reglam = new Datos();
                
                reglam.setid(resultSet.getInt(1));
                reglam.setapellido(resultSet.getString(2));
                reglam.setdir(resultSet.getString(3));
                reglam.setemail(resultSet.getString(4));
                reglam.setnombre(resultSet.getString(5));
                reglam.settel(resultSet.getString(6));
                
                per.add(reglam);
                
            }
            return per;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
            
        }
    }
    
    @Override
    public Datos listafiltro(String filtro) {
        try {
            CallableStatement statement = connection.prepareCall("{call FiltroCLientes(?)}");
            ResultSet resultSet = statement.executeQuery();
            statement.setString(1, filtro);
            Datos h = new Datos();
            
            h.setid(resultSet.getInt(1));
            h.setapellido(resultSet.getString(2));
            h.setdir(resultSet.getString(3));
            h.setemail(resultSet.getString(4));
            h.setnombre(resultSet.getString(5));
            h.settel(resultSet.getString(6));
            return h;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
            
        }
    }

    @Override
    public ArrayList<Datos> listatodo() {
        ArrayList<Datos> per = new ArrayList<Datos>();
        try {
            CallableStatement statement = connection.prepareCall("{call listacliente()}");
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Datos reglam = new Datos();
                
                reglam.setid(resultSet.getInt(1));
                reglam.setapellido(resultSet.getString(2));
                reglam.setdir(resultSet.getString(3));
                reglam.setemail(resultSet.getString(4));
                reglam.setnombre(resultSet.getString(5));
                reglam.settel(resultSet.getString(6));
                
                per.add(reglam);
                
            }
            return per;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
            
        }
    }
    
    @Override
    public ArrayList<Datos> Nombre(String filtro) {
        ArrayList<Datos> ListaNombre = new ArrayList<Datos>();
        try {
            CallableStatement statement = connection.prepareCall("{call NombreClientes(?)}");
            ResultSet resultSet = statement.executeQuery();
            statement.setString(1, filtro);
            Datos h = new Datos();
            
            h.setid(resultSet.getInt(1));
            h.setapellido(resultSet.getString(2));
            h.setdir(resultSet.getString(3));
            h.setemail(resultSet.getString(4));
            h.settel(resultSet.getString(5));
            ListaNombre.add(h);
            
            return ListaNombre;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
            
        }
    }
    
    @Override
    public ArrayList<Datos> Email(String filtro) {
        ArrayList<Datos> ListaEmail = new ArrayList<Datos>();
        try {
            CallableStatement statement = connection.prepareCall("{call NombreClientes(?)}");
            ResultSet resultSet = statement.executeQuery();
            statement.setString(1, filtro);
            Datos h = new Datos();
            
            h.setid(resultSet.getInt(1));
            h.setapellido(resultSet.getString(2));
            h.setdir(resultSet.getString(3));
            h.settel(resultSet.getString(4));
            h.setnombre(resultSet.getString(5));
            ListaEmail.add(h);
            
            return ListaEmail;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
            
        }
    }
    
    @Override
    public ArrayList<Datos> Numero(String filtro) {
        ArrayList<Datos> ListaNumero = new ArrayList<Datos>();
        try {
            CallableStatement statement = connection.prepareCall("{call NombreClientes(?)}");
            ResultSet resultSet = statement.executeQuery();
            statement.setString(1, filtro);
            Datos h = new Datos();
            
            h.setid(resultSet.getInt(1));
            h.setapellido(resultSet.getString(2));
            h.setdir(resultSet.getString(3));
            h.setemail(resultSet.getString(4));
            h.setnombre(resultSet.getString(5));
            ListaNumero.add(h);
            
            return ListaNumero;
        } catch (SQLException ex) {
            ex.printStackTrace();
            return null;
            
        }
    }
}
